package com.qbm.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qbm.association.bean.UserTypeEnum;
import com.qbm.association.entity.Administrator;
import com.qbm.association.mapper.AdministratorMapper;
import com.qbm.association.service.AdministratorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qbm.association.service.PasswordService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aron
 * @since 2022-02-07
 */
@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator> implements AdministratorService {

    @Resource
    private AdministratorMapper administratorMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private PasswordService passwordService;

    @Override
    public Administrator getByUsername(String username) {
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        Administrator administrator  = administratorMapper.selectOne(queryWrapper);
        return administrator;
    }

    @Override
    public void register(Administrator administrator, String password) {
        boolean isTrue = administratorMapper.insert(administrator)==1;
        Assert.isTrue(isTrue,"注册失败");
        isTrue = passwordService.insertUserPassword(administrator.getId(), UserTypeEnum.ADMIN,password);
        Assert.isTrue(isTrue,"注册失败");

    }

    @Override
    public void add(Map<String, Object> para) {
        Administrator administrator = new Administrator();
        administrator.setName((String) para.get("name"));
        administrator.setUserName((String) para.get("userName"));
        administrator.setAssociation((String) para.get("association"));
        administrator.setPhone((String) para.get("phone"));
        String password = (String) para.get("password");
        this.register(administrator,password);
    }


}
